﻿using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.Security.Claims;
using Volo.Abp.Uow;

namespace Hang.Blog.Web.IdentityCustomer {
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AbpUserClaimsPrincipalFactory))]
    public class MyUserClaimsPrincipalFactory :
           AbpUserClaimsPrincipalFactory, ITransientDependency {
        private readonly IHttpContextAccessor _httpContext;
        public MyUserClaimsPrincipalFactory(
            UserManager<Volo.Abp.Identity.IdentityUser> userManager,
            RoleManager<Volo.Abp.Identity.IdentityRole> roleManager,
            IOptions<IdentityOptions> options,
            ICurrentPrincipalAccessor currentPrincipalAccessor,
            IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory,
            IHttpContextAccessor httpContext)
            : base(
                userManager,
                roleManager,
                options,
                currentPrincipalAccessor,
                abpClaimsPrincipalFactory
                ) {
            _httpContext = httpContext;
        }

        [UnitOfWork]
        public override async Task<ClaimsPrincipal> CreateAsync(Volo.Abp.Identity.IdentityUser user) {
            var principal = await base.CreateAsync(user);
            principal.Identities
                .First().AddClaim(new Claim("userName", user.UserName));
            return principal;
        }


    }
}
